package com.xcity.game.employee.config.impl;

import java.util.ArrayList;
import java.util.List;

import com.xcity.db.entity.Employee;
import com.xcity.db.entity.Player;
import com.xcity.game.combat.attr.CombatAttributeType;
import com.xcity.game.employee.config.AbstractConfigure;
import com.xcity.game.employee.config.Configure;
import com.xcity.game.employee.template.EmployeeQualityUpTemplate;
import com.xcity.game.player.PlayerTrainService;

import gnu.trove.map.TIntObjectMap;
import gnu.trove.map.hash.TIntObjectHashMap;
import naga.x.App;
import naga.x.game.template.TemplateService;

/**
 * 员工升品配置
 * @author yang.li
 *
 */
public class EmployeeQualityLevelUpConfigure extends AbstractConfigure {
	
	private TIntObjectMap<List<EmployeeQualityUpTemplate>> templates; // key: id
	
	public EmployeeQualityLevelUpConfigure(Configure parent) {
		super(parent);
	}
	
	@Override
	public EmployeeQualityLevelUpConfigure configure() {
		List<EmployeeQualityUpTemplate> list = App.getApp().getServiceManager().get(TemplateService.class).getTemplatesList(EmployeeQualityUpTemplate.class);
		TIntObjectMap<List<EmployeeQualityUpTemplate>> templates = new TIntObjectHashMap<>();
		for (int i = 0; i < list.size(); i++) {
			EmployeeQualityUpTemplate template = list.get(i);
			List<EmployeeQualityUpTemplate> group = templates.get(template.getId());
			if (group == null) {
				group = new ArrayList<EmployeeQualityUpTemplate>(20); // 最高品级20?
				templates.put(template.getId(), group);
			}
			group.add(template);
		}
		this.templates = templates;
		return this;
	}

	/**
	 * 获取玩家/员工下一品级升品模板
	 * @param unit
	 * @return
	 */
	public EmployeeQualityUpTemplate getNextLevelTemplate(QualityUpgradable unit) {
		List<EmployeeQualityUpTemplate> list = templates.get(unit.getTemplateId());
		int nowLevel = unit.getQualityLevel();
		return list != null && nowLevel >= 0 && nowLevel < list.size() ? list.get(nowLevel) : null;
	}
	
	/**
	 * 获取玩家/员工最高品级
	 * @param employee
	 * @return
	 */
	public int getMaxLevel(QualityUpgradable unit) {
		List<EmployeeQualityUpTemplate> list = templates.get(unit.getTemplateId());
		return list != null ? list.size() : 0;
	}
	
	public List<EmployeeQualityUpTemplate> getTemplates(int templateId) {
		return templates.get(templateId);
	}
	
	/** 可升品的对象 */
	public static interface QualityUpgradable {
		long getId();
		int getLevel(); // 该对象等级
		int getTemplateId();
		int getQualityLevel();
		boolean addQualityLevel(int val, EmployeeQualityUpTemplate template, String cause);
		float getAttributeValue(CombatAttributeType type);
		Object reference(); // 实际引用的对象
	}
	
	/** 玩家封装成一个可升品的对象 */
	public static class PlayerWrapper implements QualityUpgradable {
		private Player player;
		public PlayerWrapper(Player player) {
			this.player = player;
		}
		@Override
		public long getId() {
			return player.getId();
		}
		@Override
		public int getLevel() {
			return player.getLevel();
		}
		@Override
		public int getTemplateId() {
			return PlayerTrainService.QUALITY_LEVEL_UP_TEMPLATE_ID;
		}
		@Override
		public int getQualityLevel() {
			return player.getQualityLvl();
		}
		@Override
		public boolean addQualityLevel(int val, EmployeeQualityUpTemplate template, String cause) {
			return player.addQualityLevel(val, template, cause);
		}
		@Override
		public float getAttributeValue(CombatAttributeType type) {
			return player.getAttributeValue(type);
		}
		@Override
		public Player reference() {
			return player;
		}
	}
	
	/** 员工封装成一个可升品的对象 */
	public static class EmployeeWrapper implements QualityUpgradable {
		private Employee employee;
		public EmployeeWrapper(Employee employee) {
			this.employee = employee;
		}
		@Override
		public long getId() {
			return employee.getId();
		}
		@Override
		public int getLevel() {
			return employee.getLevel();
		}
		@Override
		public int getTemplateId() {
			return employee.getTemplate().getUpQualityId();
		}
		@Override
		public int getQualityLevel() {
			return employee.getQualityLvl();
		}
		@Override
		public boolean addQualityLevel(int val, EmployeeQualityUpTemplate template, String cause) {
			return employee.addQualityLevel(val, template, cause);
		}
		@Override
		public float getAttributeValue(CombatAttributeType type) {
			return employee.getAttributeValue(type);
		}
		@Override
		public Employee reference() {
			return employee;
		}
	}
	
}
